<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+ 新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                {{ (page.page - 1) * page.pagesize + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currentChange"
            />
          </div>
        </el-tab-pane>
        <!-- lazy element tabs组件自带属性 延迟渲染 切换显示才渲染 -->
        <el-tab-pane label="公司信息" lazy>
          <!-- 动态组件使用 is后面的值代表要使用的组件 -->
          <component :is="'Info'"></component>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <DialogItem
      ref="dialogItem"
      :is-show.sync="isShow"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    />
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import DialogItem from './components/dialogItem.vue'
export default {
  components: {
    DialogItem,
    // 异步懒加载注册
    Info: () => import('./components/info.vue')
  },
  data () {
    return {
      isShow: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: []
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log(res)
    },
    // 页码改变时
    currentChange (page) {
      this.page.page = page
      this.getData()
    },
    // 页容量修改
    sizeChange (size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 新增角色
    add () {
      this.isShow = true
      this.$refs.dialogItem.mode = 'add'
    },
    // 修改
    edit (row) {
      this.isShow = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    // 删除
    del (row) {
      this.$confirm('确定要删除吗？', '删除角色')
        .then(async () => {
          await sysRoleDel(row.id)
          this.currentChange(1)
          this.$message.success('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;

  .box .el-form-item {
    width: 550px;
  }
}
</style>
